package com.xiaoqi.lk07_service_3_call;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.Nullable;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ListenCallService extends Service {

    //得到电话管理对象
    private TelephonyManager telephonyManager;

    public ListenCallService() {
        Log.i("callService", "ListenCallService构造器");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // 得到电话管理器对象
        telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        // 监听电话状态
        telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private PhoneStateListener listener = new PhoneStateListener() {


        @Override
        public void onCallStateChanged(int state, String phoneNumber) {

            switch (state) {
                // 空闲（挂断电话之后/未来电之前）
                case TelephonyManager.CALL_STATE_IDLE:
                Log.i("callService","挂断电话之后/未来电之前");
                    break;
                    // 响铃
                case TelephonyManager.CALL_STATE_RINGING:
                    Log.i("callService","响铃");
                    if ("110".equals(phoneNumber)){
                        try {
                            endCall();
                        } catch (ClassNotFoundException e) {
                            e.printStackTrace();
                        } catch (NoSuchMethodException e) {
                            e.printStackTrace();
                        } catch (InvocationTargetException e) {
                            e.printStackTrace();
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                    // 接通
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.i("callService","接通");
                    break;
            }
            super.onCallStateChanged(state, phoneNumber);
        }

        private void endCall() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

            // 通过反射调用隐藏的API
            // 得到隐藏类的Class对象
            Class c = Class.forName("android.os.ServiceManager");
            // 得到方法对应的method对象
            Method getService = c.getMethod("getService", String.class);
            // 调用方法
            IBinder binder = (IBinder) getService.invoke(null,Context.TELEPHONY_SERVICE);
                    // 得到接口对象
//           ITelephony telephony = ITelephony.Stud.asInterface(binder);
//            // 结束同行
//            telephony.endCall;
        }
    };

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 停止电话监听
        telephonyManager.listen(listener, PhoneStateListener.LISTEN_NONE);
    }
}
